package com.mhc.haval.security.model;

import com.mhc.haval.security.constant.Authorities;
import com.mhc.haval.security.dto.DeptAreaDTO;
import lombok.Data;
import org.springframework.security.core.GrantedAuthority;

import java.util.*;

/**
 * @author Churry
 * @create 2017-08-30 17:09
 **/
@Data
public class User extends org.springframework.security.core.userdetails.User {

    private static final long serialVersionUID = 1L;

    /**
     * 自增主键，员工ID
     * ac_staff.staff_id
     */
    private Long staffId;

    /**
     * 部门ID
     * ac_staff.dept_id
     */
    private Long deptId;

    /**
     * 员工工号
     * ac_staff.staff_no
     */
    private String staffNo;

    /**
     * 员工姓名，真名
     * ac_staff.staff_name
     */
    private String staffName;

    /**
     * 员工昵称
     * ac_staff.staff_nickname
     */
    private String staffNickname;

    /**
     * 员工邮箱，作为登录账号用
     * ac_staff.email
     */
    private String email;

    /**
     * 登录密码，密文存储，不可逆
     * ac_staff.password
     */
    private String password;

    /**
     * 员工手机号，可以作为登录账号使用
     * ac_staff.phone
     */
    private String phone;

    /**
     * 账号状态：-1 已删除，0 正常， 1 停用
     * ac_staff.status
     */
    private Integer status;

    /**
     * 部门名称
     */
    private String deptName;

    /**
     * 公司编号
     * ac_company.company_no
     */
    private String companyNo;

    /**
     * 银行ID
     */
    private Long bankId;

    /**
     * 用户拥有的角色
     */
    private List<Role> roleList;

    /**
     * 用户拥有的权限
     */
    private List<Func> funcList;

    /**
     * 部门所属区域
     */
    private List<DeptArea> deptBizAreaList;

    //实现uderdetails接口需要的属性与方法
    private Set<GrantedAuthority> authorities;

    /**
     * Calls the more complex constructor with all boolean arguments set to {@code true}.
     */
    public User(String username, String password,
                Set<? extends GrantedAuthority> authorities) {
        super(username, password, true, true, true, true, authorities);
        this.password = password;
        this.authorities = Collections.unmodifiableSet(authorities);
    }

    @Override
    public String getUsername() {
        return this.getPhone();
    }

    @Override
    public boolean isAccountNonExpired() {
        return!this.getStatus().equals(Authorities.USER_EXPIERD);
    }

    @Override
    public boolean isAccountNonLocked() {
        return !this.getStatus().equals(Authorities.USER_LOCKED);
    }

    @Override
    public boolean isCredentialsNonExpired() {
        return true;
    }

    @Override
    public boolean isEnabled() {
        return true;
    }

    @Override
    public void eraseCredentials() {
        this.setPassword(null);
    }

    /**
     * 用户名（手机号）相等即认为是同一个用户
     * @param rhs
     * @return
     */
    @Override
    public boolean equals(Object rhs) {
        return rhs instanceof User && this.phone.equals(((User) rhs).getPhone());
    }

    /**
     * Returns the hashcode of the {@code username}.
     */
    @Override
    public int hashCode() {
        return this.phone.hashCode();
    }

}
